#ifndef CONN_TAB_H
#define CONN_TAB_H
#include "fw4.h"
#include "fw4_add.h"
#include <linux/time.h>
#define NUM_OF_STATES 4
#define CONV_NOT_FOUND NULL
#define CONV_EXPIRED -2
#define BAD_STATE -1
#define BAD_COMMAND -1
typedef enum{
	CLIENT = 0,
	SERVER = 1,
	BOTH = 2,
} side_t;

/* Struct for the action table, state_t and side_t */

typedef struct{
	// tcp bits for next state
	__u8 syn:1;
	__u8 ack:1;
	__u8 fin:1;
	__u8 rst:1;	
	__u8 side:4; // side	
	// next state
	tcp_conn_t nextState; 	
} actionCell_t;


typedef enum{
	PRE_START = 0,
	SYN_SENT = 1,
	CONN_SYN_ACK = 2,
	CONN_ESTAB = 3,
	CLOSE_CLIENT = 4,
	CLOSE_SERVER = 5,
	CLOSED = 6,
	NUM_OF_TCP_STATES = 7,	
}tcp_states_t;  


typedef struct{
	// tcp bits for next state
	__u8 side; // side	
	tcp_states_t nextState; 	
} cell_t;

typedef struct{
	// tcp bits for next state
	__u8 syn:1;
	__u8 ack:1;
	__u8 fin:1;
	__u8 rst:1;	
	__u8 side:4; // side	
} tcpConv_t;

typedef enum{
	SYN = 0,
	SYN_ACK =1,
	DATA = 2,
	CLIENT_FIN = 3,		
	SERVER_FIN = 4,
	FIN_ACK = 5,
	RESET = 6,
	NUM_OF_COMMANDS = 7,	
}tcp_commands_t;  


typedef struct{
	// tcp bits for next state
	__u8 syn:1;
	__u8 ack:1;
	__u8 fin:1;
	__u8 rst:1;	
	__u8 side:4; // side	
} tcpHeaderParams_t;

int numOfConversations(void);
connection_t* findConversation(rawPacket_t* packet);
void setState(connection_t* conv, tcp_states_t state);
int addConversation(rawPacket_t* packet);
tcp_states_t nextState(tcp_states_t currentState, tcp_commands_t command, side_t side);
tcp_commands_t getCommand(tcpHeaderParams_t params);
void clearConnectionTable(void);
#endif

